package com.summer.user.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.summer.core.annotations.ScopeType;
import com.summer.core.controller.EntityQuery;
import com.summer.user.model.Role;
import com.summer.user.model.User;

@Component
@Scope(ScopeType.VIEW)
public class UserList extends EntityQuery<User>{

	private final String HQL = "select user from User user";
	private final String COUNT_HQL = "select count(user) from User user order by user.status desc";
	
	
	private List<User> selectedUsers;
	public UserList() {
		
		
		setHql(HQL);
		setCountHql(COUNT_HQL);
		final String[] RESTRICTIONS = { "user.id like #{userList.example.id}",
				"user.username like #{userList.example.username}",
				"user.mobileNumber like #{userList.example.mobileNumber}",
				"user.status like #{userList.example.status}",
				"user in elements(role.users) and role.id = #{userList.example.roles[0].id}" };
		setRestrictionExpressionStrings(Arrays.asList(RESTRICTIONS));
	}
	
	@Override
	protected void initExample() {
		User user = new User();
		List<Role> roles = new ArrayList<Role>();
		roles.add(new Role());
		user.setRoles(roles);
		setExample(user);
	}
	
	public List<User> getSelectedUsers() {
		return selectedUsers;
	}
	public void setSelectedUsers(List<User> selectedUsers) {
		this.selectedUsers = selectedUsers;
	}
	
	
	@Override
	public User getLazyModelRowData(String rowKey) {
		List<User> users = (List<User>) getLazyModel().getWrappedData();
		for (User user : users) {
			if (user.getId().equals(rowKey)) {
				return user;
			}
		}
		return null;
	}

	@Override
	public Object getLazyModelRowKey(User user) {
		return user.getId();
	}
	
	public List<User> getAllUser() {
		return getHt().find("from User user");
	}
	
	
}
